<?php
namespace zhtfrontend\components\services\message;

use common\components\helpers\Util;
use common\models\datas\Student;
use yii\base\Action;
use yii\base\ActionEvent;
use yii\helpers\ArrayHelper;
use zhtfrontend\models\datas\ClassLesson;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassStudent;
use zhtfrontend\models\datas\Goods;

class ClassAction extends Action
{
    public $saleman_name = '无';

    const EVENT_SEND_SMS_MESSAGE = 'classsend';

    public $behaviorsData = [];


    /*
     * 点击获取验证码所进行的操作
     * 1. 获取手机号
     * 2. 生成随机验证码，以及短信内容
     * 3. 发送短信
     * 4. 成功之后保存短信码和事件至session中；失败返回错误信息
     */
    public function run($lesson_id= null,$day = 3)
    {
            $lesson = ClassLesson::findOne($lesson_id);
            $apply = ClassManage::findOne($lesson->class_id);
            $studentIds = ClassStudent::find()->select('student_id')->where(['class_manage_id'=>$apply->class_id,'status'=>1])->column();
            $customerIds = Student::find()->select('customer_id')->where(['id'=>$studentIds])->column();
            $goods = Goods::findOne($apply->associate_id);


            $data['result'] = 'success';
            $res['coures_name'] = $goods->name;
            $res['customer_id'] = $customerIds;
            $res['start_time'] = \Yii::$app->formatter->asDate($lesson->start_time);
            $res['time'] = Util::getTime($lesson->start_time) .'-'. Util::getTime($lesson->end_time);

            $this->sendSysmsg($res);//触发事件


            return json_encode($data);

    }

    //调用此方法触发发送短信的事件
    public function sendSysmsg(array $res){
        $this->trigger(self::EVENT_SEND_SMS_MESSAGE, new ActionEvent(["data"=>$res]));
    }

    //重写行为
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), $this->behaviorsData);
    }


}